<?php
declare (strict_types = 1);

namespace app\command;

use app\model\Flow;
use app\model\Hotel;
use app\service\FlowService;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Init extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('app\command\init')
            ->setDescription('初始化酒店到数据库');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('开始载入文件');
        $file = [
            '大理镇_202411.xlsx',
//            '凤仪镇_202411.xlsx',
//            '海东镇_202411.xlsx',
//            '满江街道_202411.xlsx',
//            '上关镇_202411.xlsx',
//            '双廊镇_202411.xlsx',
//            '太和街道_202411.xlsx',
//            '挖色镇_202411.xlsx',
//            '湾桥镇_202411.xlsx',
//            '喜洲镇_202411.xlsx',
//            '下关街道_202411.xlsx',
//            '银桥镇_202411.xlsx'
        ];
        //处理文件（当前只处理前6个表）
        foreach ($file as $name) {
            $filename = public_path() . 'data/' . $name;
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
            $reader->setReadDataOnly(true);
            $spreadsheet = $reader->load($filename);
            //设置月和年
            $year = 2024;
            $month = 11;
            //表名和对应的月年列
            $sheetName = ['常驻人口结构' => ['G','H'], '常驻人口画像' => ['H','I'], '客流指数' => ['J','K'], '客流画像'=>['H','I'], '业态分布' => ['E','F'], '垂类业态分布' => ['E','F'], '交通设施' => ['E','F'], '消费指数' =>['F','G'], '热力图' => ['E','F'], '竞品分析' => ['F','G']];
            foreach ($sheetName as $key => $val) {
                $sheet = $spreadsheet->getSheetByName($key);
                $rows = $sheet->getHighestRow();
                $sheet->getCell($val[0] . 1)->setValue('月');
                $sheet->getCell($val[1] . 1)->setValue('年');
                for ($i = 2; $i <= $rows; $i++) {
                    //如果首行为空那么就肯定结束了
                    if (empty($sheet->getCell('A' . $i)->getValue())) {
                        continue;
                    } else {
                        //设置数据
                        $sheet->getCell($val[0] . $i)->setValue($month);
                        $sheet->getCell($val[1] . $i)->setValue($year);
                    }
                }
            }
            $writer = new Xlsx($spreadsheet);
            $writer->save($filename);
        }
    }
}
